//
//  ZQQDownLoaderManager.m
//  ZQQDownLoader
//
//  Created by MoDao-iOS on 2017/7/18.
//  Copyright © 2017年 MoDao-iOS. All rights reserved.
//

#import "ZQQDownLoaderManager.h"
#import "NSString+ZQQMD5.h"

@interface ZQQDownLoaderManager()<NSCopying, NSMutableCopying>

/** 下载任务的字典*/
@property (nonatomic, strong) NSMutableDictionary *downLoadInfo;

@end

@implementation ZQQDownLoaderManager

static ZQQDownLoaderManager *_shareInstance;
+ (instancetype)shareInstance {
    if (_shareInstance == nil) {
        _shareInstance = [[self alloc] init];
    }
    return _shareInstance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    if (!_shareInstance) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _shareInstance = [super allocWithZone:zone];
        });
    }
    return _shareInstance;
}

- (id)copyWithZone:(NSZone *)zone {
    return _shareInstance;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _shareInstance;
}

- (NSMutableDictionary *)downLoadInfo {
    if (!_downLoadInfo) {
        _downLoadInfo = [NSMutableDictionary dictionary];
    }
    return _downLoadInfo;
}

- (void)downLoader:(NSURL *)url downLoadInfo:(DownLoadInfoType)downLoadInfo progress:(ProgressBlockType)progressBlock success:(SuccessBlockType)successBlock failed:(FailedBlockType)failedBlock {
    // 1. url加密MD5
    NSString *urlMD5 = [url.absoluteString md5];
    
    // 2. 根据urlMD5,查找相应的下载器
    ZQQDownLoader *downLoader = self.downLoadInfo[urlMD5];
    if (downLoader == nil) {
        downLoader = [[ZQQDownLoader alloc] init];
        self.downLoadInfo[urlMD5] = downLoader;
    }
    
    __weak typeof(self) weakSelf = self;
    [downLoader downLoader:url downLoadInfo:downLoadInfo progress:progressBlock success:^(NSString *filePath) {
        // 下载之后，移除下载器
        [weakSelf.downLoadInfo removeObjectForKey:urlMD5];
        // 拦截block
        successBlock(filePath);
    } failed:failedBlock];
}

- (ZQQDownLoader *)downLoadWithURL:(NSURL *)url {
    NSString *md5 = [url.absoluteString md5];
    
    ZQQDownLoader *downLoader = self.downLoadInfo[md5];
    if (downLoader) {
        [downLoader resumeCurrentTask];
        return downLoader;
    }
    
    downLoader = [[ZQQDownLoader alloc] init];
    [self.downLoadInfo setValue:downLoader forKey:md5];
    
    __weak typeof(self) weakSelf = self;
    [downLoader downLoader:url downLoadInfo:nil progress:nil success:^(NSString *filePath) {
        [weakSelf.downLoadInfo removeObjectForKey:md5];
    } failed:^{
        [weakSelf.downLoadInfo removeObjectForKey:md5];
    }];
    
    return downLoader;
}

- (ZQQDownLoader *)getDownLoaderWithURL:(NSURL *)url {
    NSString *md5 = [url.absoluteString md5];
    ZQQDownLoader *downLoader = self.downLoadInfo[md5];
    return downLoader;
}

- (void)pauseWithURL:(NSURL *)url {
    NSString *urlMD5 = [url.absoluteString md5];
    ZQQDownLoader *downLoader = self.downLoadInfo[urlMD5];
    [downLoader pauseCurrentTask];
}

- (void)resumeWithURL:(NSURL *)url {
    NSString *urlMD5 = [url.absoluteString md5];
    ZQQDownLoader *downLoader = self.downLoadInfo[urlMD5];
    [downLoader resumeCurrentTask];
}

- (void)cancelWithURL:(NSURL *)url {
    NSString *urlMD5 = [url.absoluteString md5];
    ZQQDownLoader *downLoader = self.downLoadInfo[urlMD5];
    [downLoader cacelCurrentTask];
}

- (void)cancelAndClearWithURL: (NSURL *)url {
    NSString *urlMD5 = [url.absoluteString md5];
    ZQQDownLoader *downLoader = self.downLoadInfo[urlMD5];
    [downLoader cacelAndClean];
}

- (void)pauseAll {
    [self.downLoadInfo.allValues performSelector:@selector(pauseCurrentTask) withObject:nil];
}

- (void)resumeAll {
    [self.downLoadInfo.allValues performSelector:@selector(resumeCurrentTask) withObject:nil];
}


@end
